package org.opens.util;

/**
 * 功能描述
 *
 * @author: zhangyuming
 * @date: 2024/3/27 14:39
 */
public class CronUtil {

    public static final String DAY = "day";
    public static final String HOUR = "hour";
    public static final String MINUTE = "minute";
    public static final String SECOND = "second";

    /**
     * 根据数值与单位生成cron表达式
     *
     * @param value    单位的值
     * @param timeUnit 单位
     */
    public static String generateCronExpression(int value, String timeUnit) {
        // 初始化 Cron 表达式字符串
        String cronExpression = "";

        // 根据时间单位设置 Cron 表达式的不同部分
        switch (timeUnit.toLowerCase()) {
            case "day":
            case "days":
            case "3":
            case "天":
                cronExpression = "0 0 0 1/" + value + " * ?";
                break;
            case "hour":
            case "hours":
            case "2":
            case "时":
                cronExpression = "0 0 0/" + value + " * * ?";
                break;
            case "minute":
            case "minutes":
            case "1":
            case "分":
                cronExpression = "0 0/" + value + " * * * ?";
                break;
            case "second":
            case "seconds":
            case "0":
            case "秒":
                cronExpression = "0/" + value + " * * * * ?";
                break;
            default:
                throw new IllegalArgumentException("非法单位：" + timeUnit);
        }

        return cronExpression;
    }

}
